import { useState, useEffect } from 'react';
import { StyleSheet, Text, View, ScrollView, Pressable, ActivityIndicator } from 'react-native';
import { useLocalSearchParams, router, Stack } from 'expo-router';
import { format } from 'date-fns';
import { ptBR } from 'date-fns/locale';
import { Card, SubtleInfoCard } from '@/components/ui/Cards';
import { Badge } from '@/components/ui/Badge';
import { Button } from '@/components/ui/Button';
import { Col, Between, Row } from '@/components/ui/LayoutHelpers';
import { Section, SectionHeader } from '@/components/ui/Sections';
import { Theme, Colors, Spacing } from '@/constants/theme';
import { getMood } from '@/constants/moods';
import { getMoodComponent, intensityLabel, intensityToValue } from '@/constants/mood-components';
import { useMoodEntry, useDeleteMoodEntry } from '@/hooks';
import { getTimeBadge, formatMoment } from '@/lib/utils/time';
import { MoodComponentCard } from '@/components/ui/MoodComponentCard';
export default function MoodDetailScreen() {
const { id } = useLocalSearchParams<{ id: string }>();
const { data: entry, isLoading, isError } = useMoodEntry(id);
const deleteMoodEntry = useDeleteMoodEntry();
const [minutesLeft, setMinutesLeft] = useState(0);
const [canDelete, setCanDelete] = useState(false);
if (isLoading) {
return (
);
}
if (isError || !entry) {
return (
Registro não encontrado.
router.back()} style={styles.backButton}>
Voltar
);
}
const moment = new Date(entry.moment);
const moodDef = getMood(entry.selectedMood.toLowerCase());
const badge = getTimeBadge(moment);
useEffect(() => {
const updateTimer = () => {
const moment = new Date(entry.moment);
const createdAt = new Date(entry.moment);
const msSinceCreation = Date.now() - createdAt.getTime();
const deletionWindowMs = 5 * 60 * 1000;
const newCanDelete = msSinceCreation < deletionWindowMs;
const newMinutesLeft = Math.max(0, Math.ceil((deletionWindowMs - msSinceCreation) / 60000));
setCanDelete(newCanDelete);
setMinutesLeft(newMinutesLeft);
};
updateTimer(); // Set initial values
const interval = setInterval(updateTimer, 1000);
return () => clearInterval(interval);
}, [entry.moment]);
const handleDelete = async () => {
await deleteMoodEntry.mutateAsync(String(entry.id));
router.back();
};
const deleteButtonMsg = () => {
const msg = "Excluir Registro";
const timeLeftStr = `(Disponível por ${minutesLeft < 1 ? '<1' : minutesLeft}:00)`;
const suffix = canDelete ? timeLeftStr : "(Expirado)"
return `${msg} ${suffix}`
}
return (
<>
{/* Header card */}
{moodDef?.icon ?? '😐'}
{moodDef?.label ?? entry.selectedMood}
{formatMoment(moment)}
{/* Core levels */}
{[
{ label: 'Energia', value: entry.energyLevel },
{ label: 'Estresse', value: entry.stressLevel },
{ label: 'Ansiedade', value: entry.anxietyLevel },
].map((item, index, arr) => (
{item.label}
{item.value}/10
))}
{entry.moodComponents.map((comp, index) => {
const def = getMoodComponent(comp.component.toLowerCase());
const numericIntensity = comp.intensity === 'LIGHT' ? 2 : comp.intensity === 'MODERATE' ? 5 : 8;
return (
);
})}
{/* Annotation / notes */}
{entry.annotation && (
"{entry.annotation?.trim()}"
)}
{/* Delete */}
GESTÃO DO REGISTRO
>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: Theme.colors.background,
},
content: {
padding: Spacing.containerPadding,
gap: Spacing.sectionGap,
paddingBottom: 48,
},
centered: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
gap: 12,
},
errorText: {
fontSize: 15,
color: Colors.light.textSecondary,
},
backButton: {
paddingHorizontal: 20,
paddingVertical: 10,
backgroundColor: Theme.colors.tint,
borderRadius: Theme.borderRadius.md,
},
backButtonText: {
fontWeight: '700',
color: Theme.colors.text,
},
// Header
headerCard: {
padding: 16,
},
moodIcon: {
fontSize: 40,
},
moodLabel: {
fontSize: 17,
fontWeight: '700',
color: Colors.light.text,
},
moodTime: {
fontSize: 13,
color: Colors.light.textSecondary,
},
// Sections
section: {
gap: 8,
},
sectionLabel: {
fontSize: 11,
fontWeight: '600',
letterSpacing: 0.8,
color: Colors.light.textSecondary,
textTransform: 'uppercase',
},
// Components
componentRow: {
flexDirection: 'row',
justifyContent: 'space-between',
alignItems: 'center',
paddingVertical: 10,
},
componentLabel: {
fontSize: 15,
fontWeight: '700',
color: Colors.light.text,
},
componentIntensity: {
fontSize: 13,
color: Colors.light.textSecondary,
},
componentIntensityGreen: {
fontSize: 13,
fontWeight: '700',
color: Theme.colors.tint,
},
progressTrack: {
height: 6,
backgroundColor: Theme.colors.light.divider,
borderRadius: Theme.borderRadius.full,
marginBottom: 4,
overflow: 'hidden',
},
progressFill: {
height: '100%',
backgroundColor: Theme.colors.light.tint,
borderRadius: Theme.borderRadius.full,
},
// Annotation
annotation: {
fontSize: 14,
lineHeight: 22,
color: Colors.light.textSecondary,
fontStyle: 'italic',
padding: 4,
},
});